package cz.jm.web.manhunter.client.common;

import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.Label;

import cz.jm.web.manhunter.client.ResorcesInfo;

public class MessagePanel implements Panel {
	
	public enum MessageType { DEFAULT, INFO, WARNING, ERROR, OK, KO }
	
	private static final String MESSAGE_FONT_SIZE = "15px";
	private static final int PADDING = 5; 
	
	private final Label label = new Label();
	private String message = "";
	private static final int ICON_SIZE = 64;
	
	public MessagePanel() {
		this("", null);
	}
	
	public MessagePanel(String message) {
		this(message, null);
	}
	
	public MessagePanel(String message, String iconPath) {
		init(message, iconPath);
	}
	
	protected void init(String message, String iconPath) {
		setMessage(message);
		
		label.setAutoHeight();
		label.setWidth100();
		label.setAlign(Alignment.CENTER);
		label.setPadding(PADDING);		
		setIcon(iconPath);
	}

	
	void setIcon(String iconPath, int size) {
		if (iconPath != null) {
			label.setIcon(iconPath);
			label.setIconHeight(size);
			label.setIconWidth(size);
		}
	} 
	
	void setIcon(String iconPath) {
		setIcon(iconPath, ICON_SIZE);
	}
	
	
	
	public void setMessage(String message) {
		// TODO Mozno casom prerobit na konfigurovatelne nastavenie bold a font-size.
		String bold = "<b style=\"font-size:" + MESSAGE_FONT_SIZE +";\">";
		String boldEnd = "</b>";
		this.message = message;
		label.setContents(bold + this.message + boldEnd);
	}
	
	@Override
    public Canvas asCanvas() {
	    return label;
    }
	
	
	public MessagePanel getMessagePanel(MessageType type) {
		return getMessagePanel(type);
	}
	
	public static MessagePanel getMessagePanel(MessageType type, String message) {
		MessagePanel mp = new MessagePanel(message);
		switch (type) {
			case INFO:
				mp.setIcon(ResorcesInfo.getIconInfo());
				break;
			case WARNING:
				mp.setIcon(ResorcesInfo.getIconWarning());				
				break;
			case ERROR:				
				mp.setIcon(ResorcesInfo.getIconError());
				break;
			case OK:
				mp.setIcon(ResorcesInfo.getIconOk());
				break;
			case KO:
				mp.setIcon(ResorcesInfo.getIconKo());
				break;				
			case DEFAULT:				
			default:
				mp.setIcon(ResorcesInfo.getIconMessage());
				break;
		}		
	    return mp;
    }
}
